package map.android.baidu.carpool.utils;

import android.text.TextUtils;
import com.baidu.lbs.net.http.AsyncHttpClient;
import com.baidu.lbs.net.http.RequestParams;
import com.baidu.mapframework.api.ComAPIManager;
import com.baidu.mapframework.statistics.ControlLogStatistics;
import map.android.baidu.carpool.WebConfig;
import map.android.baidu.carpool.setting.UserInfoSetting;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import java.util.List;

public class StatisticsUtil {
    //TODO 修改统计类
	// UserdataLogStatistics.getInstance().addRecord(Page)//文件 离线统计。离线自己定义。
	// UserdataLogStatistics.getInstance().addTimelyRecord(strAction)//实时统计。
	// 统计组。
	// ControlLogStatistics.getInstance() //基线统计。
	// http://client.map.baidu.com/

	private static StatisticsUtil mStat = new StatisticsUtil();

	private AsyncHttpClient mHttpClient;

	public static final String DA_MODEL_KEY = "da_model";
	public static final String DA_MODEL_VALUE = "plugin_carpool";
	
	public static final String DA_TRD_KEY = "da_trd";
	public static final String DA_TRD_VALUE = "carpool";

	public static final String DA_ACT_KEY = "da_act";
	public static final String DA_ACT_VALUE_SHOW = "show";
	public static final String DA_ACT_VALUE_CLICK = "click";

	public static final String DA_SRC_KEY = "da_src";

    /**
     * 核心页面  在线
      */
    public static final String DA_SRC_VALUE_MAIN_PAGE = "carpomainpg";
    public static final String DA_SRC_VALUE_AWAIT_ORDER_PAGE = "carpoawaitorderpg";
    public static final String DA_SRC_VALUE_ORDER_DETAIL_PAGE = "carpoorderdetailpg";
    public static final String DA_SRC_VALUE_ORDER_DETAIL_UNPAID = "carpoorderdetailpg.unpaidstatus";
    public static final String DA_SRC_VALUE_ORDER_DETAIL_PAID = "carpoorderdetailpg.paidstatus";


    /**
     * 登陆与支付 离线
     */
	public static final String OFFLINE_ACT_LOGIN_PAGE = "carpologinpg";
    public static final String OFFLINE_ACT_LOGIN_SUCCESS_PAGE = "carpologinsuccesspg";
    public static final String OFFLINE_ACT_PAY_PAGE = "carpopaypg";

    /**
     * 动作 支付页面
     */
    public static final String OFFLINE_ACT_DISCOUNT_BT = "discountbt";
    public static final String OFFLINE_ACT_PAY_BT = "paybt";
    /**
     * 动作 首页
     */
    public static final String OFFLINE_ACT_CHECK_ORDER_BT = "checkorderbt";
    public static final String OFFLINE_ACT_CREATE_ORDER_BT = "createorderbt";
    public static final String OFFLINE_ACT_USER_CENTER_BT = "usercenterbt";

    /**
     * 动作 详情页
     */
    public static final String OFFLINE_ACT_FINISH_INFO_BT = "tofinishinfobt";
    public static final String OFFLINE_ACT_RECREATE_ORDER_BT = "recreateorderbt";
    public static final String OFFLINE_ACT_CANCEL_BT = "cancelbt";
    public static final String OFFLINE_ACT_DRIVER_COMMENT_BT = "drivercommentbt";
    public static final String OFFLINE_ACT_CANTACT_DRIVER_BT = "contactdriverbt";
    public static final String OFFLINE_ACT_CONFIRM_GETON_BT = "confirmgetonbt";

    /**
     * 其他动作
     */
    public static final String OFFLINE_ACT_COMMENT_SUCCESS_PAGE = "commentsuccesspg";
    public static final String OFFLINE_ACT_SHARE_BT = "sharebt";
    public static final String OFFLINE_ACT_JOIN_DRIVER_TEAM = "joindriverteampg";
    public static final String OFFLINE_ACT_CREATE_COMMENT_PAGE = "createcommentpg";
    public static final String OFFLINE_ACT_COMMIT_COMMENT_BT = "commitcommentbt";



	public void addStatisticsOnline(String daSrc, String daAct,String daTrd) {
		mHttpClient.get(getUrl(), new StatRequestParams(daSrc, daAct , daTrd), null);
	}

    public void addStatisticsOnline(String daSrc) {
        mHttpClient.get(getUrl(), new StatRequestParams(daSrc, DA_ACT_VALUE_SHOW , DA_TRD_VALUE), null);
    }

	private StatisticsUtil() {
		mHttpClient = new AsyncHttpClient();
		mHttpClient.setTimeout(30 * 1000);
		((DefaultHttpClient) (mHttpClient.getHttpClient()))
				.setHttpRequestRetryHandler(null);
	}

	public static StatisticsUtil getInstance() {
		return mStat;
	}

	private String getUrl() {
		return WebConfig.STATISTICS_DOMAIN;
	}

	class StatRequestParams extends RequestParams {
		String mDASrc;
		String mDAAct;
		String mDATrd;
		public StatRequestParams(String daSrc, String daAct ,String daTrd) {
			super();
			mDASrc = daSrc;
			mDAAct = daAct;
			mDATrd = daTrd;
		}

		@Override
		protected List<BasicNameValuePair> getParamsList() {
			addUniversalParams();
			List<BasicNameValuePair> lparams = super.getParamsList();
			return lparams;
		}

		private void addUniversalParams() {
			// UserdataLogStatistics.getInstance().addRecord(Page)//文件
			// 离线统计。离线自己定义。
			// UserdataLogStatistics.getInstance().addTimelyRecord(strAction)//实时统计。
			// 统计组。
			// ControlLogStatistics.getInstance() //基线统计。
			// http://cp01-sys-ra09-place09.cp01.baidu.com:8222/

			if (!TextUtils.isEmpty(UserInfoSetting.getCuid())
					&& !urlParams.containsKey("cuid")) {
				put("cuid", UserInfoSetting.getCuid());
			}
//			if (!TextUtils.isEmpty(UserInfoSetting.getBduss())
//					&& !urlParams.containsKey("bduss")) {
//				put("bduss", UserInfoSetting.getBduss());
//			}
			if (!TextUtils.isEmpty(UserInfoSetting.getCityId())
					&& !urlParams.containsKey("city_code")) {
				put("city_code", UserInfoSetting.getCityId());
			}
			if (!TextUtils.isEmpty(UserInfoSetting.getProductType())
					&& !urlParams.containsKey("product_type")) {
				put("product_type", UserInfoSetting.getProductType());
			}
			if (!TextUtils.isEmpty(UserInfoSetting.getOS())
					&& !urlParams.containsKey("os")) {
				put("os", UserInfoSetting.getOS());
			}
			if (!TextUtils.isEmpty(UserInfoSetting.getOSVersion())
					&& !urlParams.containsKey("version")) {
				put("version", UserInfoSetting.getOSVersion());
			}
			// zhangwei19@baidu.com 增加统计字段。
			if (!TextUtils.isEmpty(ComAPIManager.getComAPIManager()
					.getSystemAPI().getChannel())
					&& !urlParams.containsKey("channel")) {
				put("channel", ComAPIManager.getComAPIManager().getSystemAPI()
						.getChannel());
			}
			if (!TextUtils.isEmpty(ComAPIManager.getComAPIManager()
					.getSystemAPI().getResId())
					&& !urlParams.containsKey("resid")) {
				put("resid", ComAPIManager.getComAPIManager().getSystemAPI()
						.getResId());
			}
			if (!TextUtils.isEmpty(ComAPIManager.getComAPIManager()
					.getSystemAPI().getAppVersion())
					&& !urlParams.containsKey("sv")) {
				put("sv", ComAPIManager.getComAPIManager().getSystemAPI()
						.getAppVersion());
			}
			if (!TextUtils.isEmpty(ComAPIManager.getComAPIManager()
					.getSystemAPI().getDeviceName())
					&& !urlParams.containsKey("mb")) {
				put("mb", ComAPIManager.getComAPIManager().getSystemAPI()
						.getDeviceName());
			}
			//put(DA_TRD_KEY, DA_TRD_VALUE);
			put(DA_TRD_KEY, mDATrd);
			put(DA_MODEL_KEY, DA_MODEL_VALUE);
			put(DA_SRC_KEY, mDASrc);
			put(DA_ACT_KEY, mDAAct);
		}
	}

    public void addStatisticsOffline(String pg, String act){
        ControlLogStatistics.getInstance().addArg("da_model", "plugin_carpool");
        ControlLogStatistics.getInstance().addArg("cat", "carpool");
        if(TextUtils.isEmpty(act)) {
            ControlLogStatistics.getInstance().addLog(pg);
        }else{
            ControlLogStatistics.getInstance().addLog(pg + "." + act);
        }
    }

}
